const https = require('https')
const http = require('http')
const cheerio = require('cheerio')
const path = require('path')
const fs = require('fs')

const dirpath = path.join(__dirname, 'pics')

/* let url = 'http://img.jj20.com/up/allimg/1114/051621111A4/210516111A4-1-lp.jpg'
http.get(url, ret => {
  let name = path.basename(url)
  ret.pipe(fs.createWriteStream(dirpath + '/' + name))
}) */

// 数据抓取
http.get('http://www.jj20.com/bz/nxxz/', res => {

  let html = ''
  res.on('data', chunk => html += chunk)
  res.on('end', () => {
    // 把当前html内容，使用cheerio来完成选择加载s
    const $ = cheerio.load(html)
    const imgs = $('.g-box-1200 > .picbz > li > a > img')
    imgs.each((index, el) => {
      let src = $(el).attr('src')
      if (src) {
        http.get(src, ret => {
          let name = path.basename(src)
          ret.pipe(fs.createWriteStream(dirpath + '/' + name))
        })
      }
    })

  })
})



